package sjtu;

import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;

public class Sjtu1029 {
	public void go(){
		Scanner scan = new Scanner(System.in);
		int n,x,y;
		String str;
		List<String> list = new LinkedList<>();
		String result;
		while(scan.hasNext()){
			n = Integer.parseInt(scan.nextLine());
			result = "";
			for (int i = 0; i < n; i++) {
				str = scan.nextLine();
				list.add(str);
			}
			for (int i = 0; i < n - 1; i++) {
				y = scan.nextInt() - 1;
				x = scan.nextInt() - 1;
				result = list.get(x);
				result = result +" "+ list.get(y);
				list.set(x, result);
				if(i >= n - 2){
					System.out.println(list.get(x));
				}
			}
			
			list.clear();
		}
	}
	
	public static void main(String[] args) {
		new Sjtu1029().go();
	}

}
